package com.jie.state;

import com.jie.annotation.DemoEventListenerResult;
import com.jie.entity.Article;
import com.jie.enums.ArticleFlowEvent;
import com.jie.enums.ArticleFlowStateEnum;
import com.jie.mapper.ArticleMapper;
import com.jie.po.req.ArticleReq;
import com.jie.utils.BeanCopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;
import org.springframework.stereotype.Component;

import static com.jie.constants.RabbitMQConstant.ARTICLE_PUBLISH_EXCHANGE;
import static com.jie.constants.RabbitMQConstant.ARTICLE_PUBLISH_KEY;

@Component
@WithStateMachine
@Slf4j
public class ArticleStateMachineListener {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @OnTransition(source = "CREATED", target = "DESENSITIZATION")
    @DemoEventListenerResult(key = StateConstants.REPLACE)
    public void onDesensitization(Message<ArticleFlowEvent> message) {
        Article article = (Article) message.getHeaders().get("article");
        article.setStatus(ArticleFlowStateEnum.DESENSITIZATION.getValue());
        articleMapper.updateById(article);
        log.info("文章已完成脱敏状态:{}", article);
    }

    @OnTransition(source = "DESENSITIZATION", target = "COMPLETED")
    @DemoEventListenerResult(key = StateConstants.AUDIT)
    public void onAudit(Message<ArticleFlowEvent> message) {
        Article article = (Article) message.getHeaders().get("article");
        if (article != null) {
            article.setStatus(ArticleFlowStateEnum.COMPLETED.getValue());
        }
        articleMapper.updateById(article);
        log.info("文章已进入审核状态:{}", article);
        //内部改为审核成功或审核失败 通过rabbitmq发送审核结果并且可以通过状态机对数据库表进行更新状态
        rabbitTemplate.convertAndSend(ARTICLE_PUBLISH_EXCHANGE, ARTICLE_PUBLISH_KEY, article);
    }

    @OnTransition(source = "COMPLETED", target = "COMPLETED_SUCCESS")
    @DemoEventListenerResult(key = StateConstants.AUDIT_SUCCESS)
    public void onAuditSuccess(Message<ArticleFlowEvent> message) {
        Article article = (Article) message.getHeaders().get("article");
        if (article != null) {
            article.setStatus(ArticleFlowStateEnum.COMPLETED_SUCCESS.getValue());
        }
        articleMapper.updateById(article);
        log.info("文章审核成功{}",article);
    }

    @OnTransition(source = "COMPLETED", target = "DESENSITIZATION")
    @DemoEventListenerResult(key = StateConstants.AUDIT_FAIL)
    public void onAuditFail(Message<ArticleFlowEvent> message) {
        Article article = (Article) message.getHeaders().get("article");
        if (article != null) {
            article.setStatus(ArticleFlowStateEnum.DESENSITIZATION.getValue());
        }
        articleMapper.updateById(article);
        log.info("文章审核失败{}",article);
    }

    @OnTransition(source = "COMPLETED_SUCCESS", target = "PUBLISHED")
    @DemoEventListenerResult(key = StateConstants.PUBLISH)
    public void onPublish(Message<ArticleFlowEvent> message) {
        Article article = (Article) message.getHeaders().get("article");
        if (article != null) {
            article.setStatus(ArticleFlowStateEnum.PUBLISHED.getValue());
        }
        articleMapper.updateById(article);
        log.info("文章发布成功{}",article);
    }
}
